<?php

namespace App\Controller\Common;

use App\Base\BaseController;
use BaseExt\QRcode;
use App\Model\LazyCommon\PlatConfigModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class CommonController extends BaseController
{
    /**
     * @route("/test",name="commTest")
     */
    public function commTest(){
        $post = $_POST;
        $get = $_GET;
        $request = $_REQUEST;
        return $this->json_out(1,"1",array(
            "post"    => $post,
//            "get"    => $get,
//            "request"    => $request,
            "good"    => $get["goods"][0]["name"],
//            "tips"    => $get["tips"],
        ));
    }


    /**
     * @Route("/getSessId" ,name="getSessId")
     */
    public function getSessId(Request $request)
    {
        return $this->json_out(1,"OK",session_id());
    }

    /**
     * @Route("/Qr" ,name="Qr")
     */
    public function Qr()
    {
        if(!$code = $this->getParamRequired("code")){return $this->json_out(0,"参数缺失");}
        $errorCorrectionLevel = 'L';//容错级别
        $matrixPointSize = 12;//生成图片大小
        //生成二维码图片
        QRcode::png($code, false, $errorCorrectionLevel, $matrixPointSize, 2);
        die;
    }

    /**
     * @Route("/updateLocation" ,name="updateLocation")
     */
    public function updateLocation()
    {
        if (!$lat = $this->getParamRequired("lat") ) { $lat = 0; die; }
        if (!$lng = $this->getParamRequired("lng") ) { $lng = 0; die;}
        if (!$time = $this->getParamRequired("time") ) { $time = 0; die;}
        if(!$t = $this->getParamRequired("t")){$t = 99;}

        if ($lat < 0 || $lat > 180) {
            die;
        }
        if ($lng < 0 || $lng > 180) {
            die;
        }

        if ($time > time() * 100) {
            $time = floor($time/1000);
            $t = 98;
        } else {
            $t = 99;
        }

        $session = $this->mySession();
        $session->set("lat",$lat);
        $session->set("lng",$lng);
        return $this->json_out(1,"OK","");

    }

    /**
     * @Route("/app/lwnp" ,name="lwnp")
     */
    public function lwnp()
    {
        if(!$code = $this->getParamRequired("c")){return $this->json_out(0,"参数错误：249832");}
        if(!$time = $this->getParamRequired("t")){return $this->json_out(0,"参数错误：165456");}
        if(!$phone = $this->getParamRequired("p")){return $this->json_out(0,"参数错误：584983");}
        $login = 0;
        $ParentsModel = new ParentsModel($this->getDoctrine());
        $user = $ParentsModel->getByPhone($phone);
        if ($user) {
            $password = $user->getPassword();
            //string 生成：$phone + md5(md5($password))  $password为原始明文密码，数据库取出的为已经加密过的
            $string = $phone . $password;
            $chk = $this->checkVICode($code,$time,$string);
            if ($chk) {
                //如果正确模拟登陆
                $sessionData["user"] = array(
                    "id"    => $user->getId(),
                    "phone" => $user->getPhone()
                );
                $this->setSessionData($sessionData);
                $login = 1;
            }
        }
        if ($login) {
            return $this->json_out(1,"加密验证通过");
        } else {
            return $this->json_out(203,"用户尚未登录或者登录超时");
        }
    }

    /**
     * @Route("/app/update" ,name="appUpdate")
     */
    public function appUpdate()
    {
        if(!$key = $this->getParamRequired("key")){return $this->json_out(0,"系统错误");}
        switch ($key) {
            case "version":
                if(!$v = $this->getParamRequired("v")){return $this->json_out(0,"系统错误，请联系管理员");}
                if(!$app = $this->getParamRequired("app")){return $this->json_out(0,"系统错误，请联系管理员");}
                $PlatConfigModel = new PlatConfigModel($this->getDoctrine());
                if ($app == "s") {//商户 s
                    $conf = $PlatConfigModel->get(1);
                } else {//用户 u
                    $conf = $PlatConfigModel->get(2);
                }
                $version = $conf->getText();

                if ($version == $v) {
                    $update = 0;
                } else {
                    $update = 1;
                }

                $url = strip_tags($conf->getArticle());

                return $this->json_out(1,"OK",array(
                    "version"       => $version,
                    "url"           => $url,
                    "update"        => $update,
                ));
                break;
        }

        return $this->json_out(1,"");

    }

}
